home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / adv_texture / texgen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.8 KB  |  242 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* texgen.c
  19.  * This program reads in a texture from a .rgb file, sets up the
  20.  * texture and the texture environment,  then applies the texture
  21.  * to a polygon using automatically generated texture coordinates.
  22.  *
  23.  *    <g> key        - toggles between eye and object linear modes
  24.  *    Escape Key    - exit program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32.  
  33. #include "rgbImageFile.h"    /* should be in ../../include */
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  animate( GLvoid );
  39. GLvoid  visibility( GLint );
  40. GLvoid  drawScene( GLvoid );
  41. GLvoid  reshape( GLsizei, GLsizei );
  42. GLvoid  keyboard( GLubyte, GLint, GLint );
  43. GLvoid  initialize( int, char ** );
  44.  
  45. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  46. GLvoid  toggleTexGenMode( GLvoid );
  47.  
  48. GLvoid  printHelp( char * );
  49.  
  50. /* Global Definitions */
  51.  
  52. #define KEY_ESC    27    /* ascii value for the escape key */
  53.  
  54. /* Global Variables */
  55.  
  56. static GLfloat spin = 1.0;
  57.  
  58. static GLuint         mode = GL_OBJECT_LINEAR;
  59. static GLenum         plane = GL_OBJECT_PLANE;
  60.  
  61. void
  62. main ( int argc, char *argv[])
  63. {
  64.     char        *imageFileName = "bricks.rgb";
  65.     unsigned int     *image;
  66.     GLsizei        width, height, imageWidth, imageHeight;
  67.     
  68.     glutInit( &argc, argv );
  69.  
  70.     if (argc < 2) {
  71.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  72.     } else
  73.         imageFileName = argv[1];
  74.  
  75.     fprintf( stdout, "using image %s\n", imageFileName );
  76.  
  77.     image = rgbReadImageFile(imageFileName, &imageWidth,
  78.              &imageHeight);
  79.  
  80.     /* create a window that is 1/4 the size of the screen */
  81.  
  82.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  83.     height = glutGet( GLUT_SCREEN_HEIGHT );
  84.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  85.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  86.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  87.     glutCreateWindow( argv[0] );
  88.  
  89.     initTexture( image, imageWidth, imageHeight );
  90.     initgfx();
  91.  
  92.     glutKeyboardFunc( keyboard );
  93.     glutIdleFunc( animate );
  94.     glutVisibilityFunc( visibility );
  95.     glutReshapeFunc( reshape );
  96.     glutDisplayFunc( drawScene ); 
  97.  
  98.     printHelp( argv[0] );
  99.  
  100.     glutMainLoop();
  101. }
  102.  
  103. GLvoid
  104. printHelp( char *progname )
  105. {
  106.     fprintf(stdout, "\n%s - demonstrates automatic texture coordinate "
  107.         "generation\n"
  108.         "<g> key     - toggle texture coordinate generation mode\n"
  109.         "Escape key     - exit the program\n\n",
  110.         progname );
  111.     fprintf(stdout, "Using %s linear coordinate generation\n", 
  112.         (mode == GL_OBJECT_LINEAR ? "OBJECT":"EYE"));
  113. }
  114.  
  115.  
  116. GLvoid initgfx( GLvoid )
  117. {
  118.     glEnable( GL_DEPTH_TEST );
  119.  
  120.     glClearColor( 0, 0, 0, 1 );
  121. }
  122.  
  123. GLvoid initTexture( unsigned int *image, 
  124.     GLsizei imageWidth, GLsizei imageHeight )
  125. {
  126.     /* scale texture image, make mipmaps and load texture
  127.      *    gluBuild2DMipmaps( target, components, width, height,
  128.      *        format,  type, imageArray ) 
  129.      */
  130.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  131.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  132.                         
  133.     /* Set texture coordinate generation function for both
  134.      * the s and t planes to object linear mode.  Texture
  135.      * will remain attached to the object.
  136.      */
  137.     glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode );
  138.     glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode );
  139.  
  140.     /* Enable automatic texture coordinate generation for 
  141.      * both the s and t planes
  142.      */
  143.     glEnable( GL_TEXTURE_GEN_S );
  144.     glEnable( GL_TEXTURE_GEN_T );
  145.  
  146.     glEnable( GL_TEXTURE_2D );
  147. }
  148.  
  149. GLvoid
  150. toggleTexGenMode( GLvoid )
  151. {
  152.     if ( mode == GL_OBJECT_LINEAR ) {
  153.         mode = GL_EYE_LINEAR;
  154.         plane = GL_EYE_PLANE;
  155.     } else { 
  156.         mode = GL_OBJECT_LINEAR;
  157.         plane = GL_OBJECT_PLANE;
  158.     }
  159.  
  160.     fprintf(stdout, "Using %s linear coordinate generation\n", 
  161.         (mode == GL_OBJECT_LINEAR ? "OBJECT":"EYE"));
  162.  
  163.     glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode );
  164.     glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode );
  165. }
  166.  
  167. GLvoid 
  168. keyboard( GLubyte key, GLint x, GLint y )
  169. {
  170.     switch (key) {
  171.     case 'g':    /* toggle coordinate generation mode */
  172.         toggleTexGenMode();
  173.         glutPostRedisplay();
  174.         break;
  175.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  176.         exit(0);
  177.     }
  178. }
  179.  
  180. GLvoid 
  181. animate( GLvoid )
  182. {
  183.     spin = fmodf( spin + 5.0, 360.0 );
  184.  
  185.     /* Tell GLUT to redraw the scene */
  186.     glutPostRedisplay();
  187. }
  188.  
  189. GLvoid
  190. visibility( int state ) 
  191. {
  192.     if (state == GLUT_VISIBLE) {
  193.         glutIdleFunc( animate );
  194.     } else {
  195.         glutIdleFunc( NULL );
  196.     }
  197. }
  198.  
  199. GLvoid
  200. reshape( GLsizei width, GLsizei height )
  201. {
  202.     GLdouble     aspect;
  203.  
  204.     glViewport( 0, 0, width, height );
  205.  
  206.     aspect = (GLdouble) width / (GLdouble) height;
  207.  
  208.     glMatrixMode( GL_PROJECTION );
  209.     glLoadIdentity();
  210.     gluPerspective( 45.0, aspect, 3.0, 15.0 );
  211.     glMatrixMode( GL_MODELVIEW );
  212.     glLoadIdentity();
  213.     glTranslatef( 0.0, 0.0, -6.0 ); 
  214. }
  215.  
  216. GLvoid
  217. drawScene(void)
  218. {
  219.     /* Define s and t planes */
  220.     static GLfloat sgenparams[] = { 2, 0, 0, 0 };
  221.     static GLfloat tgenparams[] = { 0, 1, 0, 0 };
  222.  
  223.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  224.  
  225.     /* Specify texture coordinate generation planes */
  226.     glTexGenfv( GL_S, plane, sgenparams );
  227.     glTexGenfv( GL_T, plane, tgenparams );
  228.  
  229.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  230.     glPushMatrix();
  231.         glRotatef( spin, 0.0, 0.0, 1.0 );
  232.         glRotatef( spin, 0.0, 1.0, 0.0 );
  233.         glutSolidTorus( 0.25, 0.75, 32, 32 );
  234.  
  235.         glRotatef( 90.0, 1.0, 0.0, 0.0 );
  236.         glutSolidTorus( 0.25, 0.75, 32, 32 );
  237.     glPopMatrix();
  238.  
  239.     glutSwapBuffers();
  240. }
  241.  
  242.